<?php
if(!defined('IN_KKFRAME')) exit();
$_K['tid'] = $tid = intval($_GET['id']);
if(!$tid) showmessage('帖子不存在！', referer());
$thread = DB::fetch_first("SELECT * FROM thread WHERE tid='{$tid}'");
if(!$thread) showmessage('帖子不存在！', referer());
$description = $thread['desc'];
$nav = array($_K['config']['forums'][$thread['fid']] => 'forum.php?fid='.$thread['fid'], $thread['subject'] => 'thread.php?id='.$tid);
$amount = DB::result_first("SELECT COUNT(*) FROM post WHERE tid='{$tid}'");
if(!$amount) showmessage('帖子不存在！', referer());

if($_POST['contents']){		// Reply
	if(!$uid) showmessage('请先登陆后再发帖', 'member.php?do=login');
	$message = htmlspecialchars(daddslashes(trim($_POST['contents'])));
	if(!$message) showmessage('请输入回复内容', referer());
	if(strlen($message) < 6) showmessage('帖子内容太短，请返回补充', referer());
	if(strlen($message) >= 500) showmessage('帖子过长，请分段发布，谢谢', referer(), 5);
	if(!$_K['member']['adminid']){
		$user = DB::fetch_first("SELECT * FROM user WHERE uid='{$_K[uid]}'");
		if(!$user){
			dsetcookie('auth');
			showmessage('您的账号已被删除，无法回复', referer());
		}
		if($user['ban']) showmessage('您已经被禁止回复', referer());
		$last_reply = DB::result_first("SELECT MAX(time) FROM post WHERE author='{$_K[uid]}'");
		if($last_reply > TIMESTAMP - 15) showmessage('您回复太频繁了，请稍后再发', referer());
	}
	$amount = DB::result_first("SELECT COUNT(*) FROM post WHERE tid='{$tid}'");
	$amount++;
	$last_page = ceil($amount / 10);
	if($rpid = intval($_POST['replyto'])){
		$post_position = DB::result_first("SELECT COUNT(*) FROM post WHERE tid='{$tid}' AND pid<='{$rpid}'");
		$reply_page = ceil($post_position / 10);
		$post = DB::fetch_first("SELECT * FROM post WHERE pid='{$rpid}'");
		if(!$post) showmessage('您回复的楼层不存在！', referer());
		$message = "回复 <i><a href=\"thread.php?id={$tid}&page={$reply_page}&pid={$rpid}\" target=\"_blank\">{$post_position} 楼</a> {$post[author_name]} 的帖子</i>：<br><span class=\"reply\">{$message}</span>";
	}
	$pid = DB::insert('post', array(
		'tid'			=> $tid,
		'content'		=> $message,
		'author'		=> $_K['uid'],
		'author_name'	=> addslashes($_K['member']['username']),
		'time'			=> TIMESTAMP,
	));
	DB::update('thread', array('lastpost' => TIMESTAMP), "tid='{$tid}'");
	if($rpid){
		send_notice($post['author'], "{$_K[member][username]} 回复了<a href=\"thread.php?id={$tid}&page={$reply_page}&pid={$rpid}\" target=\"_blank\">您在帖子《{$thread[subject]}》中的回复</a><br><a href=\"thread.php?id={$tid}&page={$last_page}&pid={$pid}\" target=\"_blank\">点击这里查看</a>");
	}
	if($thread['author'] != $_K['uid'] && $post['author'] != $thread['author']) send_notice($thread['author'], "{$_K[member][username]} 回复了您的帖子《<a href=\"thread.php?id={$tid}\" target=\"_blank\">{$thread[subject]}</a>》<br><a href=\"thread.php?id={$tid}&page={$last_page}&pid={$pid}\" target=\"_blank\">点击这里查看</a>");
	showmessage('回复成功', "thread.php?id={$tid}&page={$last_page}&pid={$pid}", 0);
}

$max_page = ceil($amount / 10);
$page = max(1, min(intval($_GET['page']), $max_page));
$limit = 10*($page - 1);
$postlist = array();
$postlist = DB::fetch_all("SELECT * FROM post WHERE tid='{$tid}' ORDER BY pid LIMIT {$limit},10");
include template('thread');